package com.great.web.action;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.great.model.pojo.UserCollection;
import com.great.model.pojo.UserInfo;
import com.great.service.IUserCollectionService;
import com.great.service.IUserInfoService;
import com.great.service.IWebInfoService;
import com.great.tools.CreateYZM;
import com.sun.org.apache.bcel.internal.generic.NEW;
@Controller
public class receptionAction extends BaseAction{
	@Resource
	private IWebInfoService webInfoService;
	@Resource
	private IUserInfoService userInfoService;
	@Resource
	private IUserCollectionService userCollectionService;
	private Map<String, Object> info;
	private UserInfo userInfo;
	private UserCollection userCollection;
	private ByteArrayInputStream inputStream;
	private String YZMStr;
	public receptionAction() {
		// TODO Auto-generated constructor stub
	}
	public String getallweb() {
		try {
			info=new HashMap<String, Object>();
			info.put("allweb",webInfoService.sSelectWebByName(""));
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	public String getYZM() {
		try {
			CreateYZM createYZM=new CreateYZM();
			YZMStr=createYZM.getCode();
			session.put("YZMStr", YZMStr);
			inputStream=createYZM.getYZMStream();
		} catch (Exception e) {
			return ERROR;
		}
		
		return SUCCESS;
	}
	public String getLoginYZM() {
		try {
			CreateYZM createYZM=new CreateYZM();
			YZMStr=createYZM.getCode();
			session.put("LoginYZMStr", YZMStr);
			inputStream=createYZM.getYZMStream();
		} catch (Exception e) {
			return ERROR;
		}
		
		return SUCCESS;
	}
	public String userregister() {
		try {
			String tipstr="";
			if(userInfo.getUserName()==null)tipstr+="用户名不能为空!<br/>";
			if(userInfo.getUserNumber()==null)tipstr+="账号不能为空!<br/>";
			if(userInfo.getUserPassword()==null)tipstr+="密码不能为空!<br/>";
			
			if(userInfo.getUserName().length()==0||userInfo.getUserName().length()>10)tipstr+="用户名要求大于0小于10字符!<br/>";
			if(userInfo.getUserNumber().length()<10||userInfo.getUserName().length()>20)tipstr+="用户账号要求大于10小于20字符!<br/>";
			if(userInfo.getUserPassword().length()<10||userInfo.getUserName().length()>20)tipstr+="用户密码要求大于10小于20字符!<br/>";
			
			if(!userInfo.getUserNumber().matches("^[A-Za-z0-9]+$"))tipstr+="用户账号要求只能是数字和字母!<br/>";
			if(!userInfo.getUserPassword().matches("^[A-Za-z0-9]+$"))tipstr+="用户密码要求只能是数字和字母!<br/>";
			
			if(!YZMStr.toUpperCase().equals(session.get("YZMStr")))tipstr+="验证码错误!<br/>";
			if (tipstr.equals("")) {
				tipstr=userInfoService.sRegister(userInfo);
			}
			info.put("tipstr", tipstr);
		} catch (Exception e) {
			return ERROR;
		}
		
		return SUCCESS;
	}
	public String compareLoginYZM() {
		try {
			String tipstr="";
			if(!YZMStr.toUpperCase().equals(session.get("LoginYZMStr")))tipstr+="验证码错误!<br/>";
			else {
				tipstr=userInfoService.sLoginYZ(userInfo);
			}
			info.put("tipstr", tipstr);
		} catch (Exception e) {
			return ERROR;
		}
		
		return SUCCESS;
	}
	public String userlogin() {
		try {
			String tipstr="";
			if(YZMStr.toUpperCase().equals(session.get("LoginYZMStr"))) {
				userInfo=userInfoService.sLogin(userInfo);
			}
			if(userInfo!=null)
			{
				List<UserCollection> userCollections=userCollectionService.sgetUserCollection(userInfo);
				session.put("userInfo",userInfo);
				session.put("userCollections",userCollections);
			}
		} catch (Exception e) {
			return ERROR;
		}
	
		return SUCCESS;
	}
	public String exitlogin() {
		try {
			session.remove("userInfo");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	public String userNumberExistYZ() {
		try {
			String isexist=userInfoService.suserNumberExistYZ(userInfo);
			info.put("isexist",isexist);
		} catch (Exception e) {
			e.printStackTrace();
			return SUCCESS;
		}
		return SUCCESS;
	}
	public String addcollectionweb() {
		try {
			UserInfo temp=(UserInfo)session.get("userInfo");
			System.out.println(temp);
			userCollection.setUserId(temp.getUserId());
			session.put("userCollections",userCollectionService.saddCollection(userCollection));
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	public Map<String, Object> getInfo() {
		return info;
	}
	public void setInfo(Map<String, Object> info) {
		this.info = info;
	}
	public IWebInfoService getWebInfoService() {
		return webInfoService;
	}
	public void setWebInfoService(IWebInfoService webInfoService) {
		this.webInfoService = webInfoService;
	}
	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}
	public UserInfo getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	public String getYZMStr() {
		return YZMStr;
	}
	public void setYZMStr(String yZMStr) {
		YZMStr = yZMStr;
	}
	public IUserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(IUserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public UserCollection getUserCollection() {
		return userCollection;
	}
	public void setUserCollection(UserCollection userCollection) {
		this.userCollection = userCollection;
	}
	public IUserCollectionService getUserCollectionService() {
		return userCollectionService;
	}
	public void setUserCollectionService(IUserCollectionService userCollectionService) {
		this.userCollectionService = userCollectionService;
	}
	
}
